package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.JxCourseEntity;
import io.renren.modules.sys.service.JxCourseService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 课程
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2021-10-14 18:01:05
 */
@RestController
@RequestMapping("sys/jxcourse")
public class JxCourseController {
    @Autowired
    private JxCourseService jxCourseService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:jxcourse:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = jxCourseService.getAllWithPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:jxcourse:info")
    public R info(@PathVariable("id") Long id){
        JxCourseEntity jxCourse = jxCourseService.getById(id);

        return R.ok().put("jxCourse", jxCourse);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:jxcourse:save")
    public R save(@RequestParam Map<String, Object> params){
        JxCourseEntity jxCourse=new JxCourseEntity();
        jxCourse.setName(null!=params.get("name")?params.get("name").toString():null);
        jxCourse.setCourseTypeId(null!=params.get("courseTypeId")?Long.valueOf(params.get("courseTypeId").toString()):null);
        jxCourse.setCourseDesc(null!=params.get("courseDesc")?params.get("courseDesc").toString():null);
        jxCourse.setImgUrl(null!=params.get("imgUrl")?params.get("imgUrl").toString():null);
        jxCourse.setCourseIntroduction(null!=params.get("courseIntroduction")?params.get("courseIntroduction").toString():null);
        jxCourse.setOrderNum(null!=params.get("orderNum")? Integer.parseInt(params.get("orderNum").toString()):null);
        jxCourse.setIsHot(null!=params.get("isHot")?params.get("isHot").toString():null);
        ValidatorUtils.validateEntity(jxCourse, AddGroup.class);
        jxCourseService.save(jxCourse);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:jxcourse:update")
    public R update(@RequestParam Map<String, Object> params){
        JxCourseEntity jxCourse=new JxCourseEntity();
        jxCourse.setId(null!=params.get("id")?Long.parseLong(params.get("id").toString()):null);
        jxCourse.setName(null!=params.get("name")?params.get("name").toString():null);
        jxCourse.setCourseTypeId(null!=params.get("courseTypeId")?Long.valueOf(params.get("courseTypeId").toString()):null);
        jxCourse.setCourseDesc(null!=params.get("courseDesc")?params.get("courseDesc").toString():null);
        jxCourse.setImgUrl(null!=params.get("imgUrl")?params.get("imgUrl").toString():null);
        jxCourse.setCourseIntroduction(null!=params.get("courseIntroduction")?params.get("courseIntroduction").toString():null);
        jxCourse.setOrderNum(null!=params.get("orderNum")? Integer.parseInt(params.get("orderNum").toString()):null);
        jxCourse.setIsHot(null!=params.get("isHot")?params.get("isHot").toString():null);
        ValidatorUtils.validateEntity(jxCourse, AddGroup.class);
        ValidatorUtils.validateEntity(jxCourse,UpdateGroup.class);
        jxCourseService.updateById(jxCourse);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:jxcourse:delete")
    public R delete(@RequestBody Long[] ids){
        jxCourseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

      /** 列表
     */
    @RequestMapping("/allList")
    public R AllList(){
        return R.ok().put("list", jxCourseService.getAllCourse());
    }
}
